﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using ElectricDim;
using YArchitech.Revit;
using HYView;

namespace HYRElectric.WirePlace
{
	[Transaction(TransactionMode.Manual)]
	public class CmdWireOrthogonal : IExternalCommand
	{
		private static List<XYZ> GetChamferWireCtrlPoints(Document doc, Wire wire)
		{
			List<XYZ> list = new List<XYZ>();
			wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_TYPE).Set(0);
			doc.Regenerate();
			Options options = new Options();
			options.ComputeReferences = true;
			options.View = doc.ActiveView;
			List<GeometryObject> list2 = new List<GeometryObject>();
			foreach (GeometryObject item in wire.get_Geometry(options))
			{
				list2.Add(item);
			}
			foreach (GeometryObject geometryObject in list2)
			{
				if (geometryObject is NurbSpline)
				{
					list = (geometryObject as NurbSpline).CtrlPoints.ToList<XYZ>();
				}
				else if (geometryObject is Line)
				{
					Line line = (Line)geometryObject;
					list.Add(line.GetEndPoint(0));
					list.Add(line.GetEndPoint(1));
				}
			}
			wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_TYPE).Set(1);
			return list;
		}

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (Convert.ToDouble(commandData.Application.Application.VersionNumber) < 2015.0)
			{
				TaskDialog.Show("盈建科提示", "此功能目前只能在Revit 2015及以上版本使用。");
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			Transaction transaction = new Transaction(document, "WireOrthogonal");
			transaction.Start();
			try
			{
				ChamferWireFilter chamferWireFilter = new ChamferWireFilter();
				Reference reference = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, chamferWireFilter, "请选择需要转正的导线(只处理倒角导线)。");
				Wire wire = document.GetElement(reference) as Wire;
				if (wire == null)
				{
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Failed;
				}
				List<XYZ> chamferWireCtrlPoints = CmdWireOrthogonal.GetChamferWireCtrlPoints(document, wire);
				if (chamferWireCtrlPoints.Count == 0)
				{
					Line curve = (wire.Location as LocationCurve).Curve as Line;
					XYZ hyendPoint = curve.GetEndPoint(0);
					XYZ hyendPoint2 = curve.GetEndPoint(1);
					chamferWireCtrlPoints.Add(hyendPoint);
					chamferWireCtrlPoints.Add(hyendPoint2);
				}
				if (chamferWireCtrlPoints.Count > 3)
				{
					for (int i = 1; i < chamferWireCtrlPoints.Count - 1; i++)
					{
						chamferWireCtrlPoints[i] = ElectricalUtils.GetOrthogonalXYZ(chamferWireCtrlPoints[i - 1], chamferWireCtrlPoints[i]);
						if (i == chamferWireCtrlPoints.Count - 2)
						{
							XYZ xyz = chamferWireCtrlPoints[i - 1];
							XYZ xyz2 = chamferWireCtrlPoints[i];
							if (xyz.X == xyz2.X)
							{
								chamferWireCtrlPoints[i] = new XYZ(chamferWireCtrlPoints[i].X, chamferWireCtrlPoints[chamferWireCtrlPoints.Count - 1].Y, chamferWireCtrlPoints[i].Z);
							}
							else if (xyz.Y == xyz2.Y)
							{
								chamferWireCtrlPoints[i] = new XYZ(chamferWireCtrlPoints[chamferWireCtrlPoints.Count - 1].X, chamferWireCtrlPoints[i].Y, chamferWireCtrlPoints[i].Z);
							}
						}
					}
				}
				else if (chamferWireCtrlPoints.Count == 2)
				{
					XYZ xyz3 = new XYZ(chamferWireCtrlPoints[1].X, chamferWireCtrlPoints[0].Y, chamferWireCtrlPoints[0].Z);
					XYZ xyz4 = new XYZ(chamferWireCtrlPoints[0].X, chamferWireCtrlPoints[1].Y, chamferWireCtrlPoints[0].Z);
					double num = xyz3.DistanceTo(chamferWireCtrlPoints[1]);
					double num2 = xyz4.DistanceTo(chamferWireCtrlPoints[1]);
					if (num >= num2)
					{
						chamferWireCtrlPoints[1] = xyz4;
					}
					else
					{
						chamferWireCtrlPoints[1] = xyz3;
					}
				}
				else if (chamferWireCtrlPoints.Count == 3)
				{
					XYZ xyz5 = new XYZ(chamferWireCtrlPoints[2].X, chamferWireCtrlPoints[0].Y, chamferWireCtrlPoints[1].Z);
					XYZ xyz6 = new XYZ(chamferWireCtrlPoints[0].X, chamferWireCtrlPoints[2].Y, chamferWireCtrlPoints[1].Z);
					double num3 = xyz5.DistanceTo(chamferWireCtrlPoints[1]);
					double num4 = xyz6.DistanceTo(chamferWireCtrlPoints[1]);
					if (num3 >= num4)
					{
						chamferWireCtrlPoints[1] = xyz6;
					}
					else
					{
						chamferWireCtrlPoints[1] = xyz5;
					}
				}
				RevitVersionFuncs.SetWire(wire, chamferWireCtrlPoints);
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
